package rabbit

import (
	"fmt"
	"github.com/spf13/viper"
	"log"
)

var cfgRabbit *viper.Viper

func Init() {
	viper.AddConfigPath(".")
	err := viper.ReadInConfig()
	if err != nil {
		log.Fatal(fmt.Sprintf("Parse config file fail: %s", err.Error()))
	}
	cfgRabbit = viper.Sub("setting.rabbit")
	if cfgRabbit == nil {
		panic("config not found settings.rabbit")
	}
	RabbitConfig = InitRabbit(cfgRabbit)

}

type RabbitBinding struct {
	RouteKey string
	Queues   []string
}

type Rabbit struct {
	MqUrl    string
	Exchange string
	Binding  []RabbitBinding
}

func InitRabbit(cfg *viper.Viper) *Rabbit {
	rabbit := &Rabbit{}
	cfg.Unmarshal(rabbit)
	return rabbit
}

var RabbitConfig = new(Rabbit)

